#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
#define SIZE 4
#define SCORCE_NUM 3
class Name
{
public:
    string name; // 姓名
    int sno;     // 学号
};
class Student
{
public:
    Name name;
    float scores[SCORCE_NUM]; // 分数
    float average_score;      // 平均分
};
enum
{
    EXIT,
    PRINT,
    INPUT,
    AVERAGE
};
void InitializerStudents(Student *students, const char **names, int *snos)
{
    int student_pos, score_pos;
    for (student_pos = 0; student_pos < SIZE; student_pos++)
    {
        students[student_pos].name.name = names[student_pos];
        students[student_pos].name.sno = snos[student_pos];
        for (score_pos = 0; score_pos < SCORCE_NUM; score_pos++)
        {
            students[student_pos].scores[score_pos] = 0.0;
        }
        students[student_pos].average_score = 0.0;
    }
}
void PrintStudents(Student *students)
{
    int student_pos, score_pos;
    for (student_pos = 0; student_pos < SIZE; student_pos++)
    {
        cout << "student name: " << students[student_pos].name.name << ",student sno: " << students[student_pos].name.sno << ",scores: ";
        for (score_pos = 0; score_pos < SCORCE_NUM; score_pos++)
        {
            cout << students[student_pos].scores[score_pos] << ",";
        }
        cout << "average_score: " << students[student_pos].average_score << endl;
    }
}
void _CalculatorAverageScore(Student *students)
{
    int student_pos, score_pos;
    for (student_pos = 0; student_pos < SIZE; student_pos++)
    {
        float sum = 0;
        for (score_pos = 0; score_pos < SCORCE_NUM; score_pos++)
        {
            sum += students[student_pos].scores[score_pos];
        }
        students[student_pos].average_score = sum / SCORCE_NUM;
    }
}
bool InputStudentsScore(Student *students)
{
    bool option = true;
    while (option)
    {
        cout << "Please Enter name@: ";
        string name;
        float score;
        cin >> name;
        int student_pos = 0;
        for (student_pos = 0; student_pos < SIZE; student_pos++)
        {
            if (students[student_pos].name.name == name)
            {
                for (int score_pos = 0; score_pos < SCORCE_NUM; score_pos++)
                {
                    cout << "Please Enter score[" << score_pos << "]@: ";
                    cin >> score;
                    if (score >= 0 && score <= 100)
                    {
                        students[student_pos].scores[score_pos] = score;
                    }
                    else
                    {
                        cout << "Enter score error!!!" << endl;
                        assert(false);
                    }
                }
                break;
            }
        }
        if (student_pos == SIZE)
        {
            // 走到此处说明未找到这个姓名的学生,提示错误并退出循环
            cout << "not find this student: " << name << endl;
            return false;
        }
        // 用户选择是否继续输入
        cout << "Enter [0] exit OR Enter [1] input: " << endl;
        cin >> option;
    }
    _CalculatorAverageScore(students);
    return true;
}
void _PrintCourseAverage(float *average_course)
{
    int pos;
    for (pos = 0; pos < SCORCE_NUM; pos++)
    {
        cout << "score[" << pos << "] average score: " << average_course[pos] << endl;
    }
}
void CourseAverageScore(Student *students, float *average_course)
{
    int student_pos, score_pos;
    for (score_pos = 0; score_pos < SCORCE_NUM; score_pos++)
    {
        float sum = 0;
        for (student_pos = 0; student_pos < SIZE; student_pos++)
        {
            sum += students[student_pos].scores[score_pos];
        }
        average_course[score_pos] = sum / SIZE;
    }
    _PrintCourseAverage(average_course);
}
void PrintMenu()
{
    cout << "-------------------------------------------------------------" << endl;
    cout << "***************** 1.打印学生信息 ****************************" << endl;
    cout << "***************** 2.输入学生信息 ****************************" << endl;
    cout << "***************** 3.计算各科平均分数 ************************" << endl;
    cout << "***************** 0.退出 ***********************************" << endl;
    cout << "-------------------------------------------------------------" << endl;
}
int main()
{
    Student students[SIZE];
    // 用来初始化的数据
    const char *names[SIZE] = {"布洛妮娅", "和泉纱雾", "亚丝娜", "芙莉莲"};
    int snos[SIZE] = {101, 102, 103, 104};
    InitializerStudents(students, names, snos);
    bool flag = true;
    while (flag)
    {
        PrintMenu();
        int option;
        cout << "Enter option@> ";
        cin >> option;
        switch (option)
        {
        case EXIT:
            flag = false;
            break;
        case PRINT:
            PrintStudents(students);
            break;
        case INPUT:
        {
            bool ret = InputStudentsScore(students);
            if (ret)
                PrintStudents(students);
            break;
        }
        case AVERAGE:
            float average_course[SCORCE_NUM];
            CourseAverageScore(students, average_course);
            break;
        default:
            cout << "Enter error,exit" << endl;
            flag = false;
            break;
        }
    }
    return 0;
}